//
//  HttpManagerTask.m
//  Baymax
//
//  Created by jolin on 16/4/18.
//  Copyright © 2016年 jolin. All rights reserved.
//

#import "HttpManagerTask.h"

static NSString * const HttpURLString = @"";
@implementation HttpManagerTask

+ (instancetype)sharedHttpManager {
    static HttpManagerTask *_sharedManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedManager = [[HttpManagerTask alloc]initWithBaseURL:[NSURL URLWithString:HttpURLString]];
    });
    return _sharedManager;
}

#pragma mark - 发送请求
- (void)HttpTaskWithURLString:(NSString *)urlString requestType:(NSString *)type parameters:(NSDictionary *)parameters result:(resultFromNet)result {
    NSString *encodeString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    //self.requestSerializer = [AFJSONRequestSerializer serializer];
    //self.responseSerializer = [AFJSONResponseSerializer serializer];
    // self.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    //设置网络超时
    self.requestSerializer.timeoutInterval = 30;
    if ([type isEqualToString:@"get"] || [type isEqualToString:@"GET"]) {
        self.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",nil];
        [self GET:encodeString parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (responseObject) {
                //                NSData *data = [responseObject JSONData];
                //                NSDictionary *dict = [data objectFromJSONData];
                NSDictionary *dict = (NSDictionary *)responseObject;
                dispatch_async(dispatch_get_main_queue(), ^{
                    result(@[dict],nil);
                });
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                result(nil,error);
            });
        }];
    }else if ([type isEqualToString:@"post"] || [type isEqualToString:@"POST"]) {
        self.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",nil];
        [self POST:urlString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSDictionary *dic = (NSDictionary *)responseObject;
            if (responseObject) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    result(@[dic],nil);
                });
                
            }

        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                result(nil, error);
            });
        }];
    }else if ([type isEqualToString:@"delete"] || [type isEqualToString:@"DELETE"]) {
        [self DELETE:urlString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (responseObject) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    result(@[responseObject],nil);
                });
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                result(nil, error);
            });
        }];
    }else if ([type isEqualToString:@"put"] || [type isEqualToString:@"PUT"]) {
        [self PUT:urlString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (responseObject) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    result(@[responseObject], nil);
                });
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                result(nil, error);
            });
        }];
        
    }else {
        NSLog(@"请求格式不符合");
        return;
    }
    
}


@end
